/**
 * 封装fetch请求
 * @param url 接口地址
 * @returns {Promise<any>} 请求数据
 */
const fetchData = async (url) => {
    try {
        const res = await fetch(url, {
            cache: 'default'
        })
        if (!res.ok) {
            throw new Error(res.statusText)
        }
        return await res.json()
    } catch (e) {
        console.log(e)
        throw e
    }
}

/**
 * 获取饼图数据（评分）
 * @param url 接口地址
 */
const pie_data = async (url) => {
    const response = await fetchData(url)
    if (response.code === 200) {
        chart_manager.updateChartData(chart_list_id[0], {
            series: [{
                data: response.data
            }]
        })
    }
}

/**
 * 获取柱状图数据（评分前十）
 * @param url 接口地址
 */
const bar_data = async (url) => {
    const response = await fetchData(url)
    if (response.code === 200) {
        chart_manager.updateChartData(chart_list_id[1], {
            yAxis: [{
                data: response.data.movie_name
            }],
            series: [{
                data: response.data.score_list
            }]

        })
    }
}

/**
 * 获取散点图数据（评分与时长）
 * @param url 接口地址
 */
const scatter_data = async (url) => {
    const response = await fetchData(url)
    if (response.code === 200) {
        chart_manager.updateChartData(chart_list_id[2], {
            series: [{
                data: response.data
            }]
        })
    }
}

/**
 * 获取折线图数据（每年上映）
 * @param url 接口地址
 */
const line_data = async (url) => {
    const response = await fetchData(url)
    if (response.code === 200) {
        chart_manager.updateChartData(chart_list_id[3], {
            series: [{
                data: response.data.year_count
            }],
            xAxis: [{
                data: response.data.year_list
            }]
        })
    }
}

/**
 * 获取树图数据（电影类型）
 * @param url 接口地址
 */
const tree_data = async (url) => {
    const response = await fetchData(url)
    if (response.code === 200) {
        chart_manager.updateChartData(chart_list_id[4], {
            series: [{
                data: response.data
            }]
        })
    }
}

/**
 * 获取词云图数据（电影评论人数）
 * @param url 接口地址
 */
const word_data = async (url) => {
    const response = await fetchData(url)
    if (response.code === 200) {
        chart_manager.updateChartData(chart_list_id[5], {
            series: [{
                data: response.data
            }]
        })
    }
}


/**
 * 获取数据的更新时间
 * @param url 接口地址
 */
const data_update_time = async (url) => {
    const update_time = document.getElementById("data_update_time")
    try {
        const response = await fetchData(url)
        if (response.code === 200) {
            update_time.innerText = response.update_time
        } else {
            update_time.innerText = response.message
        }
    } catch (e) {
        update_time.innerText = "数据更新失败"
    }
}

/**
 * 随机延迟执行函数
 * @param fn 执行函数
 * @param minDelay 最小延迟
 * @param maxDelay 最大延迟
 */
function randomDelay(fn, minDelay = 100, maxDelay = 1500) {
    const delay = Math.floor(Math.random() * (maxDelay - minDelay + 1)) + minDelay;
    setTimeout(() => {
        fn();
    }, delay);
}

function chart_data_init() {
    randomDelay(() => pie_data("/api/movie/pie"));
    randomDelay(() => bar_data("/api/movie/bar"));
    randomDelay(() => scatter_data("/api/movie/scatter"));
    randomDelay(() => line_data("/api/movie/line"));
    randomDelay(() => tree_data("/api/movie/treemap"));
    randomDelay(() => word_data("/api/movie/wordcloud"));
}

randomDelay(() => data_update_time("/api/movie/update_time"))